using System;
using System.Data;
using Jalal.Library.Data;
using BCGC.DAL;
using Jalal.Library.DataStructure;
using Jalal.Library.Utility;
using BCGC.BLL.Helper;
namespace BCGC.BLL.MailManager
{

    public class PrivateMessage
    {

        private int messageID;
        public int MessageID
        {
            get
            {
                return messageID;
            }
        }

        private int senderID;
        public int SenderID
        {
            get
            {
                return senderID;
            }
            set
            {
                senderID = value;
            }
        }

        private String senderName;
        public String SenderName
        {
            get
            {
                return senderName;
            }
            set
            {
                senderName = value;
            }
        }

        private int receiverID;
        public int ReceiverID
        {
            get
            {
                return receiverID;
            }
            set
            {
                receiverID = value;
            }
        }

        private String receiverName;
        public String ReceiverName
        {
            get
            {
                return receiverName;
            }
            set
            {
                receiverName = value;
            }
        }

        private String receiverEmailID;
        public String ReceiverEmailID
        {
            get
            {
                return receiverEmailID;
            }
            set
            {
                receiverEmailID = value;
            }
        }

        private DateTime sendDate;
        public DateTime SendDate
        {
            get
            {
                return sendDate;
            }
            set
            {
                sendDate = value;
            }
        }

        private String subject;
        public String Subject
        {
            get
            {
                return subject;
            }
            set
            {
                subject = value;
            }
        }

        private String content;
        public String Content
        {
            get
            {
                return content;
            }
            set
            {
                content = value;
            }
        }

        private bool isSendEmail;
        public bool IsSendEmail
        {
            get
            {
                return isSendEmail;
            }
            set
            {
                isSendEmail = value;
            }
        }

        private bool isMarketPlaceMessage;
        public bool IsMarketPlaceMessage
        {
            get
            {
                return isMarketPlaceMessage;
            }
            set
            {
                isMarketPlaceMessage = value;
            }
        }

        private bool isRead;
        public bool IsRead
        {
            get
            {
                return isRead;
            }
            set
            {
                isRead = value;
            }
        }

        private bool isActive;
        public bool IsActive
        {
            get
            {
                return isActive;
            }
            set
            {
                isActive = value;
            }
        }

        private bool isShowInBox;

        public bool IsShowInBox
        {
            get { return isShowInBox; }
            set { isShowInBox = value; }
        }
        private bool isShowOutBox;

        public bool IsShowOutBox
        {
            get { return isShowOutBox; }
            set { isShowOutBox = value; }
        }
        private bool isReply;

        public bool IsReply
        {
            get { return isReply; }
            set { isReply = value; }
        }
        private int replyToMessageID;

        public int ReplyToMessageID
        {
            get { return replyToMessageID; }
            set { replyToMessageID = value; }
        }


        public PrivateMessage(){}

        public PrivateMessage(DataRow dataRow)
        {
            if (dataRow != null)
            {
                messageID = (int)CommonUtility.FilterNull(dataRow["MessageID"], typeof(int));
                senderID = (int)CommonUtility.FilterNull(dataRow["SenderId"], typeof(int));
                senderName = (string)CommonUtility.FilterNull(dataRow["SenderName"], typeof(string));
                receiverID = (int)CommonUtility.FilterNull(dataRow["ReceiverID"], typeof(int));
                receiverName = (string)CommonUtility.FilterNull(dataRow["ReceiverName"], typeof(string));
                receiverEmailID = (string)CommonUtility.FilterNull(dataRow["ReceiverEmailID"], typeof(string));
                sendDate = (DateTime)CommonUtility.FilterNull(dataRow["SendDate"], typeof(DateTime));
                subject = (string)CommonUtility.FilterNull(dataRow["Subject"], typeof(string));
                content = (string)CommonUtility.FilterNull(dataRow["MailContent"], typeof(string));
                isSendEmail = (bool)CommonUtility.FilterNull(dataRow["IsSendEmail"], typeof(bool));
                isMarketPlaceMessage = (bool)CommonUtility.FilterNull(dataRow["IsMarketPlaceMessage"], typeof(bool));
                isRead = (bool)CommonUtility.FilterNull(dataRow["IsRead"], typeof(bool));
                isActive = (bool)CommonUtility.FilterNull(dataRow["IsActive"], typeof(bool));
                isShowInBox = (bool)CommonUtility.FilterNull(dataRow["IsShowInBox"], typeof(bool));
                isShowOutBox = (bool)CommonUtility.FilterNull(dataRow["IsShowOutBox"], typeof(bool));
                isReply = (bool)CommonUtility.FilterNull(dataRow["IsReply"], typeof(bool));
                replyToMessageID = (int)CommonUtility.FilterNull(dataRow["ReplyToMessageID"], typeof(int));
                
            }
         }
        private static ReadWriteCustomCollection<PrivateMessage> GetCollection(DataRowCollection rowCollection)
        {
            ReadWriteCustomCollection<PrivateMessage> collection = new ReadWriteCustomCollection<PrivateMessage>();

            foreach (DataRow dr in rowCollection)
            {
                collection.Add(new PrivateMessage(dr));
            }
            return collection;
        }

        private int Create()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            int result = PrivateMessageData.CreatePrivateMessage(this.SenderID,this.SenderName,this.ReceiverID,this.ReceiverName,this.ReceiverEmailID,this.SendDate,this.Subject,this.Content,this.IsSendEmail,this.IsMarketPlaceMessage,this.IsRead,this.IsActive,this.IsShowInBox,this.IsShowOutBox,this.IsReply,this.ReplyToMessageID);
            DataFactory.GetDataMiner().CommitTransaction();
            return result;
        }

        private bool Update()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            bool result = PrivateMessageData.UpdatePrivateMessage(this.MessageID,this.SenderID, this.SenderName, this.ReceiverID, this.ReceiverName, this.ReceiverEmailID, this.SendDate, this.Subject, this.Content, this.IsSendEmail, this.IsMarketPlaceMessage, this.IsRead, this.IsActive,this.IsShowInBox,this.IsShowOutBox,this.IsReply,this.ReplyToMessageID);
            DataFactory.GetDataMiner().CommitTransaction();
            return result;
        }

        public bool Save()
        {
            if (this.MessageID != 0)
            {
                return Update();
            }
            else
            {
                int result = Create();
                if (result > 0)
                {
                    this.messageID= result;
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public static bool Delete(int id)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            bool result = PrivateMessageData.DeletePrivateMessage(id);
            DataFactory.GetDataMiner().CommitTransaction();
            return result;
        }

        public static ReadWriteCustomCollection<PrivateMessage> GetPrivateMessageBySenderID(int senderID, string sortOrder, string sortDirection, int currentPageIndex, int pageSize, out int totalRecords)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = PrivateMessageData.GetPrivateMessageBySenderID(senderID,sortOrder,sortDirection,currentPageIndex,pageSize,out totalRecords);
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return GetCollection(result.Rows);
            else
                return null;
        }

        public static ReadWriteCustomCollection<PrivateMessage> GetPrivateMessageByReceiverID(int receiverID, string sortOrder, string sortDirection, int currentPageIndex, int pageSize, out int totalRecords)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = PrivateMessageData.GetPrivateMessageByReceiverID(receiverID,sortOrder,sortDirection,currentPageIndex,pageSize,out totalRecords);
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return GetCollection(result.Rows);
            else
                return null;
        }

        public static ReadWriteCustomCollection<PrivateMessage> GetAllPrivateMessage()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = PrivateMessageData.GetAllPrivateMessage();
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return GetCollection(result.Rows);
            else
                return null;
        }


        public static PrivateMessage GetPrivateMessageByMessageID(int messageID)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = PrivateMessageData.GetPrivateMessageByMessageID(messageID);
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return new PrivateMessage(result.Rows[0]);
            else
                return null;
        }

        public static bool MailBoxOperations(string xml, MailMessageOperations options)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
             bool result =false;
             switch (options.ToString())
             {
                 case "Read": result = PrivateMessageData.SetReadMails(xml); break;
                 case "Unread": result = PrivateMessageData.SetReadMails(xml); break;
                 case "Delete": result = PrivateMessageData.SetReadMails(xml); break;
             }

            DataFactory.GetDataMiner().CommitTransaction();
            return result;

        }

    }

}
